package setting_tab

import (
	"fmt"

	"file_explorer/common/consts"
	"file_explorer/common/util"
	"file_explorer/model"
	"file_explorer/service"
	"file_explorer/service/third_program"
	"file_explorer/view/components/main_panel/fe_tab"
	"file_explorer/view/packed_widgets"
	"file_explorer/view/store"

	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
	"github.com/spf13/cast"
)

type SettingTab struct {
	fe_tab.DefaultTab

	showVirtualFile   *widget.Check
	favoritesNotExist *widget.Select
	metaFileNum       *packed_widgets.NumEntry
	logRowNum         *packed_widgets.NumEntry
	checkVersion      *packed_widgets.Label

	editForm *widget.Form

	tabCtx *store.SettingTabContext
}

func (tab *SettingTab) updateGlobalConf(confKey string, confVal interface{}) {
	err := tab.tabCtx.UpdateGlobalConf(confKey, confVal)
	if err != nil {
		packed_widgets.NotifyError("更新配置失败，错误：%s", err.Error())
		return
	}

	packed_widgets.NotifyInfo("更新配置成功")
}

func (tab *SettingTab) refreshData() {
	tab.showVirtualFile.SetChecked(tab.tabCtx.GetGlobalConf().ShowVirtualFile)
	tab.favoritesNotExist.SetSelected(consts.FavoritesNotExistAction.GetValue(tab.tabCtx.GetGlobalConf().FavoritesNotExist))
	tab.metaFileNum.SetValue(cast.ToString(tab.tabCtx.GetGlobalConf().MetaFileNum))
}

func NewSettingTab(tabCtx store.TabContext) *SettingTab {
	tab := &SettingTab{
		editForm: widget.NewForm(),
		tabCtx:   tabCtx.(*store.SettingTabContext),
	}

	// 构造组件
	tab.showVirtualFile = widget.NewCheck("", func(b bool) {
		if tab.tabCtx.GetGlobalConf().ShowVirtualFile == b {
			return
		}
		tab.updateGlobalConf(model.ConfKeyShowVirtualFile, b)
	})

	tab.favoritesNotExist = widget.NewSelect(consts.FavoritesNotExistAction.GetValues(), func(v string) {
		actionKey := consts.FavoritesNotExistAction.GetKey(v)
		if tab.tabCtx.GetGlobalConf().FavoritesNotExist == actionKey {
			return
		}
		tab.updateGlobalConf(model.ConfKeyFavoritesNotExist, actionKey)
	})

	tab.metaFileNum = packed_widgets.NewNumEntry(util.PtrInt(1), nil, nil)
	tab.metaFileNum.OnChange = func(s string) {
		num := cast.ToInt(s)
		if num < 1 || num == tab.tabCtx.GetGlobalConf().MetaFileNum {
			return
		}
		tab.updateGlobalConf(model.ConfKeyMetaFileNum, num)
	}

	tab.logRowNum = packed_widgets.NewNumEntry(util.PtrInt(1), nil, nil)
	tab.logRowNum.OnChange = func(s string) {
		num := cast.ToInt(s)
		if num < 1 || num == tab.tabCtx.GetGlobalConf().LogRowNum {
			return
		}
		tab.updateGlobalConf(model.ConfKeyLogRowNum, num)
	}

	tab.checkVersion = packed_widgets.NewLabel("")
	curVersion, latestVersion, url, err := service.CheckVersion()
	if err != nil {
		tab.checkVersion.SetText(fmt.Sprintf("%s，检查更新失败", curVersion))
	} else if curVersion == latestVersion {
		tab.checkVersion.SetText(fmt.Sprintf("%s，已经是最新版本", curVersion))
	} else if len(url) > 0 {
		tab.checkVersion.SetText(fmt.Sprintf("%s, 双击此处更新到v%s", curVersion, latestVersion))
		tab.checkVersion.OnDoubleTapped = func() {
			third_program.GetCaller().OpenUrlsWithDefaultWebExplorer([]string{url})
		}
	}

	// 初始化数据
	tab.refreshData()

	// 加入表单
	tab.editForm.Append("显示虚文件", tab.showVirtualFile)
	tab.editForm.Append("收藏夹失效时", tab.favoritesNotExist)
	tab.editForm.Append("元文件最大数量", tab.metaFileNum.Container)
	tab.editForm.Append("日志最大行数", tab.logRowNum.Container)
	tab.editForm.Append("版本", tab.checkVersion)

	tab.Container = container.NewBorder(nil, nil, nil, nil, tab.editForm)

	return tab
}
